package com.geekchique.runningbuddy.controller;

import java.util.Date;
import java.util.TreeMap;

import android.content.Context;
import android.location.Location;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.geekchique.runningbuddy.activity.Stopwatch;
import com.geekchique.runningbuddy.activity.stopwatch.StopwatchThread;
import com.geekchique.runningbuddy.activity.stopwatch.StopwatchTools;
import com.geekchique.runningbuddy.model.Jog;
import com.geekchique.runningbuddy.provider.ActivityID;
import com.geekchique.runningbuddy.util.DBHelperImpl;
import com.geekchique.runningbuddy.view.StopwatchView;
import com.geekchique.runningbuddy.view.TargetTimeView;

public class StopwatchControllerImpl implements IStopClockController{

    protected StopwatchThread timerRunnerThread;
	private StopwatchView stopwatchView;
	private TargetTimeView stopwatchTargetView;
	private Stopwatch stopwatchActivity;
    private DBHelperImpl dbHelper;


    public StopwatchControllerImpl(Stopwatch stopwatchActivity){
    	this.stopwatchActivity = stopwatchActivity;
    	dbHelper = new DBHelperImpl(stopwatchActivity.getContext());
    }


	public void createStopClockView(Context activity, TextSwitcher stopClockTextSwitcher, Button startButton) {
			stopwatchView = new StopwatchView(
				activity,
				stopClockTextSwitcher,
				startButton
			);

		stopwatchView.setStartStopButtonClickListener(executeAStart);
		stopwatchView.resetStopClock();

	}

	public void createwatchClockTargetView(Context context,
			CheckBox checkBox,
			TextView hours,
			TextView minutes,
			TextView seconds,
			TextView milliseconds,
			Button hoursUp,
			Button hoursDown,
			TextView hoursSep,
			Button minutesUp,
			Button minutesDown,
			TextView minutesSep,
			Button secsUp,
			Button secsDown,
			TextView secondsSep,
			Button millisecsUp,
			Button millisecsDown) {

	        timerRunnerThread = new StopwatchThread(stopwatchServiceHandler);

			stopwatchTargetView = new TargetTimeView(
					context,
					checkBox,
					hours,
					minutes,
					seconds,
					milliseconds,
					hoursUp,
					hoursDown,
					hoursSep,
					minutesUp,
					minutesDown,
					minutesSep,
					secsUp,
					secsDown,
					secondsSep,
					millisecsUp,
					millisecsDown,
					timerRunnerThread
			);

			stopwatchTargetView.hideSetTargetTime();
	}


	protected Handler stopwatchServiceHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ActivityID.UPDATESTOPCLOCK:
				updateRunningStopClock();
				break;
			}
			super.handleMessage(msg);
		}
	};


	public void start() {
		timerRunnerThread.start();
		stopwatchActivity.startStopwatchService();
		stopwatchView.switchStopwatchButton(ActivityID.STOPWATCH_RUNNING);
		stopwatchView.setStartStopButtonClickListener(executeAStop);
	}

	public void stop() {
		timerRunnerThread.stop();
		stopwatchActivity.stopStopClockService();
		saveJogLogEntry("jog" + new Date().getTime(), timerRunnerThread.getStopClock().getStartTime(), timerRunnerThread.getStopClock().getStopTime());
		stopwatchView.switchStopwatchButton(ActivityID.STOPCLOCK_NOT_RUNNING);
		stopwatchView.setStartStopButtonClickListener(executeAStart);
	}

	private void updateRunningStopClock() {
		String elapsedTime = timerRunnerThread.getElapsedTime();

		if (timerRunnerThread.targetIsSet() && isTargetTimeReached(elapsedTime)) {
			stop();
			stopwatchView.setStopClock(timerRunnerThread.getTargetTime());
		} else {
			stopwatchView.setStopClock(elapsedTime);
		}
	}

	private boolean isTargetTimeReached(String elapsedTime) {
		int stopClock = StopwatchTools.compare(timerRunnerThread.getTargetTime(), elapsedTime);

		return stopClock == StopwatchTools.EQUAL	|| stopClock == StopwatchTools.GREATER_THAN;
	}


	private OnClickListener executeAStart = new OnClickListener() {
		public void onClick(View v) {
			start();
		}
	};
	private OnClickListener executeAStop = new OnClickListener() {
		public void onClick(View v) {
			stop();
		}
	};


	protected void saveJogLogEntry(String name, long startTime, long stopTime) {

		TreeMap<Long, Location> locationsAndTimes = new TreeMap<Long, Location>();

		addJogTime(startTime, -4229920, 55846809, locationsAndTimes);
    	addJogTime(startTime, -4231200, 55846809, locationsAndTimes);
    	addJogTime(stopTime, -4236400, 55851200, locationsAndTimes);
    	addJogTime(stopTime, -4236800, 55851000, locationsAndTimes);


		dbHelper.addJogEntry(new Jog(name, name, locationsAndTimes));
//		dbHelper.addJogEntry(new Jog(name, startTime, stopTime));
	}

	private void addJogTime(long time, int longitude, int latitude, TreeMap<Long, Location> listOfJogTimes) {
		Location pointLocation;
		pointLocation = new Location();
    	pointLocation.setLongitude(longitude);
    	pointLocation.setLatitude(latitude);
    	listOfJogTimes.put(time, pointLocation);
	}


}
